package com.edu.service.impl;

import com.edu.bean.ReceiveTable;
import com.edu.bean.TransTable;
import com.edu.bean.TransactionTable;
import com.edu.mapper.UserMapper;
import com.edu.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author:zyn
 * @Date:2025/11/18--10:51:46
 */
@Service
@Transactional
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    private ReceiveTable rt;
    private TransTable tt;
    private TransactionTable tat;

    @Override
    public int transferMoney(int userId1, int userId2, double money) {
        rt = new ReceiveTable();
        tt = new TransTable();
        tat = new TransactionTable();
        int result1 = userMapper.modifyUserMoney(userId1, -money);
        int result2 = userMapper.modifyUserMoney(userId2, +money);
        if ((result1 + result2) == 2) {
            rt.setReceiveUserId(userId2);
            rt.setTransUserId(userId1);
            rt.setReceiveMoney(money);
            rt.setReceiveDate(new Date());
//            System.out.println(1/0);
            tt.setTransUserId(userId1);
            tt.setReceiveUserId(userId2);
            tt.setTransMoney(money);
            tt.setTransDate(new Date());

            tat.setFromAccount(userId1);
            tat.setToAccount(userId2);
            tat.setTransactionMoney(money);
            tat.setTransactionDate(new Date());

            userMapper.insertSourceTransTable(tt);
            userMapper.insertSourceReceiveTable(rt);
            userMapper.insertSourceTransactionTable(tat);
            return 1;
        } else {
            return 0;
        }
    }
}
